package com.ywgt.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.ywgt.springcloud.service.PaymentHystrixFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
@DefaultProperties(defaultFallback = "globalBack")
@RestController
@Slf4j
public class OrderHystrixController {
    @Resource
    private PaymentHystrixFeignService paymentHystrixFeignService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo(@PathVariable("id") Integer id){
        String paymentInfo = paymentHystrixFeignService.paymentInfo(id);
        log.info(paymentInfo);
        return paymentInfo;
    };

    @GetMapping("/consumer/payment/hystrix/time/{id}") //默认1秒出错，所以这里一直调用fallback方法
    @HystrixCommand(fallbackMethod = "timeoutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value ="10000")
    })
    public String timeout(@PathVariable("id") Integer id){
        String timeout = paymentHystrixFeignService.timeout(id);
        log.info(timeout);
        return timeout;
    };

    public String timeoutHandler(Integer id){
        return "timeoutHandler超时"+id+"消费者80，服务器服务繁忙";
    }

    public String globalBack(){
        return "你访问的页面出错了，程序猿小哥正在维修";
    }
}
